#ifndef __IRRECEIVE_H__
#define __IRRECEIVE_H__

#include "GlobalConfig.h"
#include "NEC.h"

#include "stm32f1xx_hal.h"
#include "ring_buffer.h"
#include <stdint.h>
#include <stdbool.h>

#define IR_QUEUE_SIZE 128

typedef struct
{
    TIM_HandleTypeDef *htim;                    // 定时器句柄
    RingBuffer_Def rawQueue;                    // 环形队列存原始时间差
    volatile bool frameReady;                   // 是否检测到前导帧
    void (*callback)(const IrFrame_Def *frame); // 解码成功回调
    uint32_t lastCapture;                       // 上次捕获值
} IRReceiver_RingHandleTypeDef;

// 初始化
bool IRReceiver_Begin(IRReceiver_RingHandleTypeDef *h, TIM_HandleTypeDef *htim);

// 停止
void IRReceiver_Stop(IRReceiver_RingHandleTypeDef *h);

// 输入捕获回调，中断调用
void IRReceiver_OnCapture(IRReceiver_RingHandleTypeDef *h, TIM_HandleTypeDef *htim);

// 轮询解码，判断前导帧
void IRReceiver_PollDecode(IRReceiver_RingHandleTypeDef *h, IrFrame_Def *frame);

extern IRReceiver_RingHandleTypeDef irReceiver;
#endif // __IRRECEIVE_RING_H__
